package com.orangelife.mobile.propertyOld.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.confirmOrder.biz.ConfirmOrderHttpBiz;
import com.orangelife.mobile.pay.activity.UsersPaidActivity;
import com.orangelife.mobile.pay.biz.PayManageImpl;
import com.orangelife.mobile.pay.biz.PaymentModule;
import com.orangelife.mobile.pay.interfaze.IOuttradenoCallBack;
import com.orangelife.mobile.property.activity.PropertyBillDetailActivity;
import com.orangelife.mobile.property.biz.PropertyHttpBiz;
import com.orangelife.mobile.propertyOld.adapter.PropertyBillDetailOldBaseInfoAdapter;
import com.orangelife.mobile.propertyOld.adapter.PropertyBillDetailOldOrderInfoAdapter;
import com.orangelife.mobile.propertyOld.biz.PropertyHttpOldBiz;
import com.orangelife.mobile.widget.ScrollListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 旧的物业缴费账单明细
 * Created by shz on 2015/12/7.Modify by wgf on 160601.
 *
 * @author shz
 */
public class PropertyBillDetailOldActivity extends OrangeLifeBaseActivity {

    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.tvTotal)
    TextView tvTotal;
    @Bind(R.id.tvPropertyNum)
    TextView tvPropertyNum;
    @Bind(R.id.tvYuan)
    TextView tvYuan;
    @Bind(R.id.tvTip)
    TextView tvTip;
    @Bind(R.id.lvPropertyBillBaseInfo)
    ScrollListView lvPropertyBillBaseInfo;
    @Bind(R.id.lvPropertyBillOrderInfo)
    ScrollListView lvPropertyBillOrderInfo;
    @Bind(R.id.btPayCost)
    Button btPayCost;
    @Bind(R.id.tvIsPay)
    TextView tvIsPay;
    /**
     * 数据源
     */
    private Map<String, Object> entityMap = new HashMap<>();
    private List<Map<String, Object>> baseInfoList = new ArrayList<>();
    private List<Map<String, Object>> orderInfoList = new ArrayList<>();
    private PropertyBillDetailOldBaseInfoAdapter mBaseInfoAdapter;
    private PropertyBillDetailOldOrderInfoAdapter mOrderInfoAdapter;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    /** 订单id */
    private String orderID;
    /*** 状态 0,未缴费 2，已缴费  3,已过期 */
    private String status;
    /** 提示语 */
    private String tip;
    /** 缴费金额 */
    private String totalCost;
    private String feetip;
    /** 1表示要显示往期账单的图标，0表示不需要显示往期账单的图标 */
    private String type;
    private String apartmentID;
    private String mCommID;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_property_bill_detail_old);
        ButterKnife.bind(this);
        getIntentInfo();
        initView();
        setAdapter();/*Adapter需要先初始化*/
        requestPropertyBillDetail();
        setListener();
    }

    private void getIntentInfo() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            orderID = bundle.getString("orderID");
            mCommID = bundle.getString("commID");
        }
    }

    /**
     * 初始化页面数据
     */
    private void initView() {
        tvTitle.setText("账单明细");
    }

    private void setAdapter() {
        mBaseInfoAdapter = new PropertyBillDetailOldBaseInfoAdapter(PropertyBillDetailOldActivity.this, baseInfoList);
        lvPropertyBillBaseInfo.setAdapter(mBaseInfoAdapter);
        mOrderInfoAdapter = new PropertyBillDetailOldOrderInfoAdapter(PropertyBillDetailOldActivity.this, orderInfoList);
        lvPropertyBillOrderInfo.setAdapter(mOrderInfoAdapter);
    }

    private void setListener() {
        btPayCost.setOnClickListener(listener);
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestPropertyBillDetail();
            }
        });
    }

    /**
     * 获取我的物业缴费信息
     */
    private void requestPropertyBillDetail() {
        PropertyHttpOldBiz.getPropertyBillDetail(orderID, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                emptyLayout.dismiss();
                entityMap = PropertyHttpOldBiz.handlePropertyBillDetail(response);
                if (entityMap != null) {
                    orderID = String.valueOf(entityMap.get("orderID"));
                    tip = String.valueOf(entityMap.get("tip"));
                    status = String.valueOf(entityMap.get("status"));
                    feetip = String.valueOf(entityMap.get("feetip"));
                    totalCost = String.valueOf(entityMap.get("totalCost"));
                    apartmentID = String.valueOf(entityMap.get("apartmentID"));
                    if (status.equals("0")) {
                        btPayCost.setVisibility(View.VISIBLE);
                        tvIsPay.setVisibility(View.INVISIBLE);
                    } else {
                        btPayCost.setVisibility(View.INVISIBLE);
                        if (status.equals("2")) {
                            tvIsPay.setVisibility(View.VISIBLE);
                        } else {
                            tvIsPay.setText("已过期");
                            tvIsPay.setVisibility(View.VISIBLE);
                            tvIsPay.setBackgroundResource(R.color.backgroudGray);
                        }
                    }
                    tvTip.setText(tip);
                    tvPropertyNum.setText(totalCost);
                    if (mBaseInfoAdapter != null) {
                        baseInfoList = (List<Map<String, Object>>) entityMap.get("baseInfo");
                        mBaseInfoAdapter.setList(baseInfoList);
                        mBaseInfoAdapter.notifyDataSetChanged();
                    }
                    if (mOrderInfoAdapter != null) {
                        orderInfoList = (List<Map<String, Object>>) entityMap.get("orderInfo");
                        mOrderInfoAdapter.setList(orderInfoList);
                        mOrderInfoAdapter.notifyDataSetChanged();
                    }
                } else {
                    emptyLayout.setErrorMessage("获取数据失败");
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                }
            }
        });
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btPayCost:
                    final ConfirmDialog Paydialog = new ConfirmDialog(PropertyBillDetailOldActivity.this, "提示", feetip, "取消", "确定");
                    Paydialog.show();
                    Paydialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doLeft() {
                            Paydialog.dismiss();
                        }

                        @Override
                        public void doRight() {
                            Paydialog.dismiss();
                            requestGenOrder();
                        }
                    });
                    break;
                default:
                    break;
            }
        }
    };

    private void requestGenOrder() {
        showDialog();
        ConfirmOrderHttpBiz.genOrderOld(mCommID, orderID, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject jsonObject, int i) {
                closeDialog();
                /*成功之后，会跳转到支付界面*/
                ConfirmOrderHttpBiz.handleGenOrder(PropertyBillDetailOldActivity.this, jsonObject, totalCost);
            }
        });
    }
}
